function addCookie(name, value, expiresHours) {
    var cookieString = name + "=" + escape(value);
    // 判断是否设置过期时间,0代表关闭浏览器时失效
    if (expiresHours > 0) {
        var date = new Date();
        date.setTime(date.getTime + expiresHours * 360 * 24000);
        cookieString = cookieString + "; expires=" + date.toGMTString();
    }
    document.cookie = cookieString + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';'); // 把cookie分割成组
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i]; // 取得字符串
        while (c.charAt(0) == ' ') { // 判断一下字符串有没有前导空格
            c = c.substring(1, c.length); // 有的话，从第二位开始取
        }
        if (c.indexOf(nameEQ) == 0) { // 如果含有我们要的name
            return unescape(c.substring(nameEQ.length, c.length)); // 解码并截取我们要值
        }
    }
    return false;
}

function changecity(cityName,areaCode) {
    addCookie('locateName', encodeURI(cityName),3600*24);
    addCookie('locate', areaCode,3600*24);
    window.location.reload();
}

function locating() {
    var map, geolocation;
    // 加载地图，调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable : true
    });
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy : true,// 是否使用高精度定位，默认:true
            timeout : 1000, // 超过10秒后停止定位，默认：无穷大
            buttonOffset : new AMap.Pixel(10, 20),// 定位按钮与设置的停靠位置的偏移量，默认：Pixel(10,20)
            zoomToAccuracy : true, // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition : 'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);// 返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);// 返回定位信息
    });
    // 解析定位结果
    function onError(data) {
        console.log("解析定位结果:错误");
        console.log(data);
    }
    function onComplete(data) {
        console.log("解析定位结果:成功");
        var key = 'a8120376ee41dbc1883529bd837d23a2';
        lnglatXY = [Number(data.position.getLng()),Number(data.position.getLat())]; //已知点坐标
        function regeocoder() {  //逆地理编码
            var geocoder = new AMap.Geocoder({
                radius: 1000,
                extensions: "all"
            });
            geocoder.getAddress(lnglatXY, function(status, result) {
                var cityCode = result.regeocode.addressComponent.citycode;
                var cityName = result.regeocode.addressComponent.city;
                console.log("城市码"+cityCode);
                console.log("城市名称"+cityName);
                var isask = getCookie(isask);
                //定位出来的城市和session的城市对比，不一样则提示切换城市
                if(cityCode != scode && isask == false){
                    layer.confirm('是否定位到当前位置？', {
                        btn: ['是','否'] //按钮
                    }, function(){
                        addCookie("locate",cityCode,3600*24);
                        addCookie("locateName",encodeURI(cityName),3600*24);
                        window.location.reload();
                    }, function(){
                        //其他页面不在询问
                        addCookie("locate",scode);
                        addCookie("locateName",encodeURI(sname),3600*24);
                    });

                }
            });
        }
        regeocoder()
    }
}

var scode = "";
var sname = "";

function locatePre(scode1,sname1){
    scode = scode1;
    sname = sname1;
    console.log("scode:"+scode,"sname:"+sname);
    var locate = getCookie("locate");
    if(locate == null || locate == ""){
        locating();
    }
}